﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrm.Services.Realization;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.AspNetCore.Routing.Matching;
using Microsoft.Data.SqlClient.DataClassification;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Win32.SafeHandles;
using Nancy.Routing.Trie;
using Newtonsoft.Json;
using NPOI.SS.Formula.Functions;

// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace FangChanCrm.Controllers
{
    public class KeHuController : Controller
    {
        private readonly It_KeHuBiaoService it_KeHuBiaoService;
        private readonly It_XiaoShouBiaoService it_XiaoShouBiaoService;
        private readonly It_XiaoShouXiangXiBiaoService it_XiaoShouXiangXiBiaoService;
        private readonly It_FangYuanBiaoService t_FangYuanBiaoService;
        private readonly It_FangYuanXinXiBiaoService t_FangYuanXinXiBiaoService;
        private readonly It_XiaoShouBiaoService t_XiaoShouBiaoService;

        public KeHuController(It_KeHuBiaoService it_KeHuBiaoService, It_XiaoShouBiaoService it_XiaoShouBiaoService, It_XiaoShouXiangXiBiaoService it_XiaoShouXiangXiBiaoService, It_FangYuanBiaoService fangYuanBiaoService, It_FangYuanXinXiBiaoService fangYuanXinXiBiaoService, It_XiaoShouBiaoService xiaoShouBiaoService, IConfiguration configuration)
        {
            this.it_KeHuBiaoService = it_KeHuBiaoService;
            this.it_XiaoShouBiaoService = it_XiaoShouBiaoService;
            this.it_XiaoShouXiangXiBiaoService = it_XiaoShouXiangXiBiaoService;
            t_FangYuanBiaoService = fangYuanBiaoService;
            t_FangYuanXinXiBiaoService = fangYuanXinXiBiaoService;
            t_XiaoShouBiaoService = xiaoShouBiaoService;
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }
        // GET: /<controller>/
        public IActionResult Detail()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                //var kehuid = Request.Query["Id"];
                //List<t_KeHuBiao> list = new List<t_KeHuBiao>() { it_KeHuBiaoService.GetById(Convert.ToInt32(Request.Query["Id"])) };
                ViewBag.KeHu = it_KeHuBiaoService.GetById(Convert.ToInt32(Request.Query["Id"]));
                ViewBag.XiaoShouXiangXi = it_XiaoShouXiangXiBiaoService.GetListWithRenYuanAndXiaoShou().Where(x => x.t_XiaoShouBiao.KeHuId == Convert.ToInt32(Request.Query["Id"])).OrderByDescending(x => Convert.ToDateTime(x.CaoZuoShiJian)).ToList();
                return View();
            }
        }
        public IActionResult KeHuList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult ListNew()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult KeHuAdd()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult KeHuUpDate()
        {
            return View();
        }
        public IActionResult BaoBei()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult GenJinList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult ShuJuZhongXin()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult KeHuFangYuan()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult KeHuBuLu()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        [HttpPost]
        public string AddKeHu()
        {
            t_KeHuBiao t_KeHuBiao = new t_KeHuBiao
            {
                KeHuXingMing = Request.Form["XingMing"].ToString(),
                ShouJiHaoMa = Request.Form["ShouJiHaoMa"].ToString(),
                YiXiangDengJi = Request.Form["YiXiangDengJi"].ToString(),
                LianMingKeHu = Request.Form["LianMingKeHu"].ToString(),
                NianLing = Request.Form["NianLing"].ToString(),
                XingBie = Request.Form["XingBie"].ToString(),
                ZhengJianLeiXing = Request.Form["ZhengJianLeiXing"].ToString(),
                ZhengJianHaoMa = Request.Form["ZhengJianHaoMa"].ToString(),
                RenZhiMeiTi = Request.Form["RenZhiMeiTi"].ToString(),
                KeHuBeiZhu = Request.Form["KeHuBeiZhu"].ToString(),
                GeRenBiaoQian = Request.Form["GeRenBiaoQian"].ToString(),
                ZhiYeShenFen = Request.Form["ZhiYeShenFen"].ToString(),
                JiaTingJieGou = Request.Form["JiaTingJieGou"].ToString(),
                JiaoYuChengDu = Request.Form["JiaoYuChengDu"].ToString(),
                HunYinZhuangTai = Request.Form["HunYinZhuangTai"].ToString(),
                ShouRuQingKuang = Request.Form["ShouRuQingKuang"].ToString(),
                GongZuoQuYu = Request.Form["GongZuoQuYu"].ToString(),
                GOngZuoDiZhi = Request.Form["GOngZuoDiZhi"].ToString(),
                JuZhuQuYu = Request.Form["JuZhuQuYu"].ToString(),
                JuZhuDiZhi = Request.Form["JuZhuDiZhi"].ToString(),
                YiXiangYongTu = Request.Form["YiXiangYongTu"].ToString(),
                YiXiangYeTai = Request.Form["YiXiangYeTai"].ToString(),
                YiXiangHuXing = Request.Form["YiXiangHuXing"].ToString(),
                YiXiangLouCeng = Request.Form["YiXiangLouCeng"].ToString(),
                YiXiangMianJi = Request.Form["YiXiangMianJi"].ToString(),
                YiXiangTaoShu = Request.Form["YiXiangTaoShu"].ToString(),
                YiXiangDanJia = Request.Form["YiXiangDanJia"].ToString(),
                YiXiangZongJia = Request.Form["YiXiangZongJia"].ToString(),
                GuanZhuNeiRong = Request.Form["GuanZhuNeiRong"].ToString(),
                BeiZhu1 = System.DateTime.Now.ToString(),
                BeiZhu2 = Request.Form["slt_kehulaiyuan"].ToString(),
                ZhuangTai = "未到访",
                RenYuanId = Convert.ToInt32(HttpContext.Session.GetString("loginid"))
            };
            if (it_KeHuBiaoService.Add(t_KeHuBiao).Id > 0)
            {
                return "success";
            }
            else
            {
                return "error";
            }
        }



        #region 房源状态显示
        [HttpPost]
        public string fangyuanzhuangtai()
        {
            var fangwuxinxi = t_FangYuanXinXiBiaoService.GetFangWuBangDing(Convert.ToInt32(Request.Form["XiaoQuId"]), Request.Form["LouHao"], Convert.ToInt32(Request.Form["Ceng"])).Where(x => x.Men == Request.Form["Men"]).ToList();
            return fangwuxinxi[0].ZhuangTai;
        }
        #endregion
        #region 小区下拉框绑定
        [HttpPost]
        public string fanyuanliebiao()
        {

            return JsonConvert.SerializeObject(t_FangYuanBiaoService.GetList(), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        #endregion
        #region  补录
        [HttpPost]
        public string chongxietianjia()
        {
            string ShouJiHaoMa = Request.Form["ShouJiHaoMa"];//手机号码
            string yewuyuan = Request.Form["yewuyuan"];//业务员
            string zhiyeguwen = Request.Form["zhiyeguwen"];//置业顾问
            string XiaoQuId = Request.Form["XiaoQuId"];//小区id
            string LouHao = Request.Form["LouHao"];//楼号
            string Ceng = Request.Form["Ceng"];//层
            string Men = Request.Form["Men"];//门
            string ChengJiaoJinE = Request.Form["ChengJiaoJinE"];//成交金额
            int panduan = 0;//判断  返回值
            int kehuId = 0;//客户Id
            int xiaoshouId = 0;//销售Id
            int fangyuanId = t_FangYuanXinXiBiaoService.GetFangWuBangDing(Convert.ToInt32(XiaoQuId), LouHao, Convert.ToInt32(Ceng)).Where(x => x.Men == Men).ToList()[0].Id;//房源Id

            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));

            while (panduan >= 0)
            {
                switch (panduan)
                {
                    case 0://判断 客户 是否已经存在
                        panduan = 2;
                        int xiaoshouchangdu = it_KeHuBiaoService.GetList().Where(x => x.ShouJiHaoMa == ShouJiHaoMa && x.YiXiangXinXi == XiaoQuId).ToList().Count();
                        if (xiaoshouchangdu > 0)
                        {
                            //获取客户对应的销售记录
                            List<t_KeHuBiao> kehubiao = it_KeHuBiaoService.GetList().Where(x => x.ShouJiHaoMa == ShouJiHaoMa && x.YiXiangXinXi == XiaoQuId).ToList();
                            for (int i = 0; i < xiaoshouchangdu; i++)
                            {
                                t_XiaoShouBiao xiaoshoubiao = kehubiao[i].t_XiaoShouBiao;
                                if (yewuyuan == "")
                                {
                                    if (xiaoshoubiao.ZhiYeGuWenId == Convert.ToInt32(zhiyeguwen))
                                    {
                                        kehuId = kehubiao[i].Id;//赋值客戶Id
                                        panduan = -2;//返回判断 返回客户Id
                                    }
                                }
                                else
                                {
                                    if (xiaoshoubiao.YeWuYuanId == Convert.ToInt32(yewuyuan) && xiaoshoubiao.ZhiYeGuWenId == Convert.ToInt32(zhiyeguwen))
                                    {
                                        kehuId = kehubiao[i].Id;//赋值客戶Id
                                        panduan = -2;//返回判断 返回客户Id
                                    }
                                }
                            }
                        }
                        break;
                    case 2:
                        //赋值客户人员
                        string renyuanId = "";
                        if (yewuyuan != "")
                        {
                            renyuanId = yewuyuan;
                        }
                        else
                        {
                            if (zhiyeguwen != "")
                            {
                                renyuanId = zhiyeguwen;
                            }
                        }
                        //客户赋值
                        t_KeHuBiao t_KeHuBiao = new t_KeHuBiao
                        {
                            KeHuXingMing = Request.Form["XingMing"],
                            ShouJiHaoMa = ShouJiHaoMa,
                            YiXiangDengJi = "",
                            LianMingKeHu = "",
                            NianLing = Request.Form["NianLing"],
                            XingBie = Request.Form["XingBie"],
                            ZhengJianLeiXing = "",
                            ZhengJianHaoMa = "",
                            RenZhiMeiTi = "",
                            KeHuBeiZhu = "",
                            GeRenBiaoQian = "",
                            ZhiYeShenFen = "",
                            JiaTingJieGou = "",
                            JiaoYuChengDu = "",
                            HunYinZhuangTai = "",
                            ShouRuQingKuang = "",
                            GongZuoQuYu = "",
                            GOngZuoDiZhi = "",
                            JuZhuQuYu = "",
                            JuZhuDiZhi = "",
                            YiXiangXinXi = XiaoQuId,
                            YiXiangYongTu = "",
                            YiXiangYeTai = "",
                            YiXiangHuXing = "",
                            YiXiangLouCeng = "",
                            YiXiangMianJi = "",
                            YiXiangTaoShu = "",
                            YiXiangDanJia = "",
                            YiXiangZongJia = "",
                            GuanZhuNeiRong = "",
                            BeiZhu1 = System.DateTime.Now.ToString(),
                            ZhuangTai = "已失效",
                            RenYuanId = Convert.ToInt32(renyuanId)
                        };
                        kehuId = it_KeHuBiaoService.Add2(t_KeHuBiao).Id;
                        //客户添加判断
                        if (kehuId > 0)
                        {
                            panduan = 3;
                        }
                        else
                        {
                            panduan = -3;
                        }
                        break;
                    case 3:
                        //销售赋值
                        t_XiaoShouBiao t_XiaoShouBiao = new t_XiaoShouBiao
                        {
                            KeHuId = kehuId,
                            ShengChengShiJian = DateTime.Now.ToString(),
                            YeWuYuanId = Convert.ToInt32(yewuyuan),
                            ZhiYeGuWenId = Convert.ToInt32(zhiyeguwen),
                            FangWuId = fangyuanId,
                            ChengJiaoJinE = ChengJiaoJinE,
                            BeiZhu1 = DateTime.Now.ToString()
                        };
                        xiaoshouId = it_XiaoShouBiaoService.Add(t_XiaoShouBiao).Id;
                        //销售添加判断
                        if (xiaoshouId > 0)
                        {
                            panduan = 4;
                        }
                        else
                        {
                            panduan = -4;
                        }
                        break;
                    case 4:
                        //销售详细报备赋值
                        t_XiaoShouXiangXiBiao t_XiaoShouXiangXiBiao = new t_XiaoShouXiangXiBiao
                        {
                            XiaoShouDongZuo = "报备",
                            CaoZuoShiJian = DateTime.Now.ToString(),
                            CaoZuoRen = Convert.ToInt32(HttpContext.Session.GetString("loginid")),
                            XiaoShouBiaoId = xiaoshouId,
                            ZhuangTai = "",
                            MiaoShu = "新增-报备"
                        };
                        //销售详细添加报备判断
                        if (it_XiaoShouXiangXiBiaoService.Add(t_XiaoShouXiangXiBiao).Id > 0)
                        {
                            //销售详细签约赋值
                            t_XiaoShouXiangXiBiao t_XiaoShouXiangXiBiao2 = new t_XiaoShouXiangXiBiao
                            {
                                XiaoShouDongZuo = "已签约",
                                CaoZuoShiJian = DateTime.Now.ToString(),
                                CaoZuoRen = Convert.ToInt32(HttpContext.Session.GetString("loginid")),
                                XiaoShouBiaoId = xiaoshouId,
                                ZhuangTai = "已签约",
                                MiaoShu = ""
                            };
                            //销售详细添加签约判断
                            if (it_XiaoShouXiangXiBiaoService.Add(t_XiaoShouXiangXiBiao2).Id > 0)
                            {
                                panduan = 5;
                            }
                            else
                            {
                                panduan = -5;
                            }
                        }
                        else
                        {
                            panduan = -5;
                        }
                        break;
                    case 5:
                        //房源客户赋值
                        t_FangYuanKeHuGuanLianBiao fykhglb = new t_FangYuanKeHuGuanLianBiao
                        {
                            KeHuId = kehuId,
                            FangYuanId = fangyuanId,
                            ChengJiaoJinE = ChengJiaoJinE,
                            XiaoShouBiaoId = xiaoshouId,
                            BeiZhu1 = "已签约",
                            BeiZhu2 = DateTime.Now.ToString()
                        };
                        //房源客户添加判断
                        if (it_XiaoShouBiaoService.fykhAdd(fykhglb).Id > 0)
                        {
                            panduan = 6;
                        }
                        else
                        {
                            panduan = -6;
                        }
                        break;
                    case 6:
                        //房屋信息修改
                        using (var _context = new MyContext(optionsBuilder.Options))
                        {
                            var FangYuanXinXi = _context.t_FangYuanXinXiBiao.FirstOrDefault(x => x.Id == fangyuanId);
                            FangYuanXinXi.ZhuangTai = "已出售";
                            if (_context.SaveChanges() > 0)
                            {
                                panduan = 7;
                            }
                            else
                            {
                                panduan = -7;
                            }
                        }
                        break;
                    default:
                        //返回成功
                        panduan = -1;
                        break;
                }
            }
            //判断客户和销售是否已有数据
            if (panduan == -2)
            {
                return "" + kehuId;
            }
            else
            {
                return "" + panduan;
            }

        }
        #endregion





        #region 数据中心 弹层列表
        [HttpPost]
        public string InitializationDataTable2()
        {
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                //房源客户
                List<t_FangYuanKeHuGuanLianBiao> fangyuangkehu = _context.t_FangYuanKeHuGuanLianBiao.Where(x => x.KeHuId == Convert.ToInt32(Request.Form["Id"])).ToList();
                //房源
                List<t_FangYuanXinXiBiao> fangyuan = _context.t_FangYuanXinXiBiao.ToList();
                //房源
                List<t_FangYuanBiao> xiaoqu = t_FangYuanBiaoService.GetList().ToList();
                //linq 拼接 json格式转换
                var asdasd = (from a in fangyuan join b in fangyuangkehu on a.Id equals b.FangYuanId join c in xiaoqu on a.XiaoQuId equals c.Id orderby b.BeiZhu1 select new { b.Id, b.FangYuanId, c.XiaoQuMingCheng, a.LouHao, a.Ceng, a.Men, a.ZhuangTai, b.ChengJiaoJinE, b.BeiZhu1 }).ToList();
                return Helper.ListToJsonAddFenYe((from a in fangyuan join b in fangyuangkehu on a.Id equals b.FangYuanId join c in xiaoqu on a.XiaoQuId equals c.Id orderby b.BeiZhu1 select new { b.Id, b.FangYuanId, c.XiaoQuMingCheng, a.LouHao, a.Ceng, a.Men, a.ZhuangTai, b.ChengJiaoJinE, b.BeiZhu1 }).ToList(), Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]));
            }
        }
        #endregion
        #region 数据中心 添加销售
        public int Tianjiaxiaoshou()
        {
            string kehuid = Request.Form["Id"];//客户Id
            string zhuangtai = Request.Form["zhuangtaiya"];//状态
            string XiaoQuId = Request.Form["XiaoQuId"];//小区id
            string LouHao = Request.Form["LouHao"];//楼号
            string Ceng = Request.Form["Ceng"];//层
            string Men = Request.Form["Men"];//门
            string ChengJiaoJinE = Request.Form["ChengJiaoJinE"];//成交金额
            int panduan = 1;
            int xiaoshouId = 0;//销售Id
            int fangyuanId = t_FangYuanXinXiBiaoService.GetFangWuBangDing(Convert.ToInt32(XiaoQuId), LouHao, Convert.ToInt32(Ceng)).Where(x => x.Men == Men).ToList()[0].Id;//房源Id


            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            var optionsBuilder2 = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder2.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));

            while (panduan >= 1)
            {
                switch (panduan)
                {
                    case 1:
                        //客户信息修改
                        using (var _context = new MyContext(optionsBuilder.Options))
                        {
                            //客户信息赋值
                            var Kehu = _context.t_KeHuBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(kehuid));
                            //判断是否需要修改客户
                            if (Kehu.ZhuangTai == zhuangtai)
                            {
                                if (Kehu.YiXiangXinXi == "")
                                {
                                    Kehu.YiXiangXinXi = XiaoQuId;
                                    //客戶判断修改
                                    if (_context.SaveChanges() > 0)
                                    {
                                        panduan = 2;
                                    }
                                    else
                                    {
                                        panduan = -2;
                                    }
                                }
                                else
                                {
                                    panduan = 2;
                                }
                            }
                            else
                            {
                                Kehu.ZhuangTai = zhuangtai;
                                Kehu.YiXiangXinXi = XiaoQuId;
                                //客戶判断修改
                                if (_context.SaveChanges() > 0)
                                {
                                    panduan = 2;
                                }
                                else
                                {
                                    panduan = -2;
                                }
                            }
                        }
                        break;
                    case 2:
                        //销售信息修改
                        using (var _context = new MyContext(optionsBuilder2.Options))
                        {
                            //客户信息赋值
                            var xiaoshou = _context.t_XiaoShouBiao.FirstOrDefault(x => x.KeHuId == Convert.ToInt32(kehuid));
                            xiaoshou.FangWuId = fangyuanId;
                            if (zhuangtai != "已退房")
                            {
                                xiaoshou.ChengJiaoJinE = (Convert.ToInt32(_context.t_FangYuanKeHuGuanLianBiao.Where(x => x.XiaoShouBiaoId == xiaoshou.Id && x.BeiZhu1 != "已退房").ToList().Sum(x => Convert.ToInt32(x.ChengJiaoJinE))) + Convert.ToInt32(ChengJiaoJinE)).ToString();
                            }
                            xiaoshou.BeiZhu1 = DateTime.Now.ToString();
                            //赋值Id
                            xiaoshouId = xiaoshou.Id;

                            //客戶判断修改
                            if (_context.SaveChanges() > 0)
                            {
                                panduan = 3;
                            }
                            else
                            {
                                panduan = -3;
                            }
                        }
                        break;
                    case 3:
                        //销售详细报备赋值
                        //t_XiaoShouXiangXiBiao t_XiaoShouXiangXiBiao = new t_XiaoShouXiangXiBiao
                        //{
                        //    XiaoShouDongZuo = "报备",
                        //    CaoZuoShiJian = DateTime.Now.ToString(),
                        //    CaoZuoRen = Convert.ToInt32(HttpContext.Session.GetString("loginid")),
                        //    XiaoShouBiaoId = xiaoshouId,
                        //    ZhuangTai = "",
                        //    MiaoShu = "新增-报备"
                        //};
                        ////销售详细添加报备判断
                        //if (it_XiaoShouXiangXiBiaoService.Add(t_XiaoShouXiangXiBiao).Id > 0)
                        //{
                        //销售详细签约赋值
                        t_XiaoShouXiangXiBiao t_XiaoShouXiangXiBiao2 = new t_XiaoShouXiangXiBiao
                        {
                            XiaoShouDongZuo = zhuangtai,
                            CaoZuoShiJian = DateTime.Now.ToString(),
                            CaoZuoRen = Convert.ToInt32(HttpContext.Session.GetString("loginid")),
                            XiaoShouBiaoId = xiaoshouId,
                            ZhuangTai = zhuangtai,
                            MiaoShu = ""
                        };
                        //销售详细添加签约判断
                        if (it_XiaoShouXiangXiBiaoService.Add(t_XiaoShouXiangXiBiao2).Id > 0)
                        {
                            panduan = 4;
                        }
                        else
                        {
                            panduan = -4;
                        }
                        //}
                        //else
                        //{
                        //    panduan = -4;
                        //}
                        break;
                    case 4:
                        //房源客户赋值
                        t_FangYuanKeHuGuanLianBiao fykhglb = new t_FangYuanKeHuGuanLianBiao
                        {
                            KeHuId = Convert.ToInt32(kehuid),
                            FangYuanId = fangyuanId,
                            ChengJiaoJinE = ChengJiaoJinE,
                            XiaoShouBiaoId = xiaoshouId,
                            BeiZhu1 = zhuangtai,
                            BeiZhu2 = DateTime.Now.ToString()
                        };
                        //房源客户添加判断
                        if (it_XiaoShouBiaoService.fykhAdd(fykhglb).Id > 0)
                        {
                            panduan = 5;
                        }
                        else
                        {
                            panduan = -5;
                        }
                        break;
                    case 5:
                        //房屋信息修改
                        using (var _context = new MyContext(optionsBuilder.Options))
                        {
                            var FangYuanXinXi = _context.t_FangYuanXinXiBiao.FirstOrDefault(x => x.Id == fangyuanId);
                            if (zhuangtai != "已签约")
                            {
                                FangYuanXinXi.ZhuangTai = "审核中";
                            }
                            else
                            {
                                FangYuanXinXi.ZhuangTai = "已出售";
                            }
                            if (_context.SaveChanges() > 0)
                            {
                                panduan = 6;
                            }
                            else
                            {
                                panduan = -6;
                            }
                        }
                        break;
                    default:
                        panduan = -1;
                        break;
                }
            }
            return panduan;
        }
        #endregion
        #region 数据中心 显示金额
        [HttpPost]
        public string Xianshijine()
        {
            return JsonConvert.SerializeObject(t_XiaoShouBiaoService.GetList().Where(x => x.KeHuId == Convert.ToInt32(Request.Form["Id"])).ToList(), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
            //return t_XiaoShouBiaoService.GetList().Where(x => x.KeHuId == Convert.ToInt32(Request.Form["Id"])).ToList()[0].ChengJiaoJinE;
        }
        #endregion
        #region 数据中心 修改房源状态
        [HttpPost]
        public int Kehuxiugai()
        {
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            var optionsBuilder0 = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder0.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            var optionsBuilder1 = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder1.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            var optionsBuilder2 = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder2.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            var optionsBuilder3 = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder3.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            int panduan = 0;
            while (panduan >= 0)
            {
                switch (panduan)
                {
                    case 0://客户修改
                        using (var _context = new MyContext(optionsBuilder.Options))
                        {
                            //客户id
                            int Id = _context.t_FangYuanKeHuGuanLianBiao.Where(x => x.Id == Convert.ToInt32(Request.Form["id"])).ToList()[0].KeHuId;
                            var kehu = _context.t_KeHuBiao.FirstOrDefault(x => x.Id == Id);
                            //判断是否需要修改客户
                            if (kehu.ZhuangTai == Request.Form["zhuangtai"])
                            {
                                panduan = 2;
                            }
                            else
                            {
                                kehu.ZhuangTai = Request.Form["zhuangtai"];
                                if (_context.SaveChanges() > 0)
                                {
                                    panduan = 2;
                                }
                                else
                                {
                                    panduan = -2;
                                }
                            }
                        }
                        break;
                    case 2://销售修改
                        if (Request.Form["zhuangtai"] != "已退房")
                        {
                            if (Request.Form["zhuangtai"] == "已签约")
                            {
                                using (var _context = new MyContext(optionsBuilder0.Options))
                                {
                                    //销售id
                                    int Id = _context.t_FangYuanKeHuGuanLianBiao.Where(x => x.Id == Convert.ToInt32(Request.Form["id"])).ToList()[0].XiaoShouBiaoId;
                                    //房源客户成交金额
                                    int ChengJiaoJinE = Convert.ToInt32(_context.t_FangYuanKeHuGuanLianBiao.Where(x => x.Id == Convert.ToInt32(Request.Form["id"])).ToList()[0].ChengJiaoJinE);
                                    var xiaoshou = _context.t_XiaoShouBiao.FirstOrDefault(x => x.Id == Id);
                                    xiaoshou.ChengJiaoJinE = (Convert.ToInt32(xiaoshou.ChengJiaoJinE) + ChengJiaoJinE).ToString();
                                    if (_context.SaveChanges() > 0)
                                    {
                                        panduan = 3;
                                    }
                                    else
                                    {
                                        panduan = -3;
                                    }
                                }
                            }
                            else
                            {
                                panduan = 3;
                            }
                        }
                        else
                        {
                            using (var _context = new MyContext(optionsBuilder0.Options))
                            {
                                //销售id
                                int Id = _context.t_FangYuanKeHuGuanLianBiao.Where(x => x.Id == Convert.ToInt32(Request.Form["id"])).ToList()[0].XiaoShouBiaoId;
                                //房源客户成交金额
                                int ChengJiaoJinE = Convert.ToInt32(_context.t_FangYuanKeHuGuanLianBiao.Where(x => x.Id == Convert.ToInt32(Request.Form["id"])).ToList()[0].ChengJiaoJinE);
                                var xiaoshou = _context.t_XiaoShouBiao.FirstOrDefault(x => x.Id == Id);
                                xiaoshou.ChengJiaoJinE = (Convert.ToInt32(xiaoshou.ChengJiaoJinE) - ChengJiaoJinE).ToString();
                                if (_context.SaveChanges() > 0)
                                {
                                    panduan = 3;
                                }
                                else
                                {
                                    panduan = -3;
                                }
                            }
                        }
                        break;
                    case 3://销售详细
                           //添加销售详细 
                        using (var _context = new MyContext(optionsBuilder1.Options))
                        {
                            t_XiaoShouXiangXiBiao t_XiaoShouXiangXiBiao = new t_XiaoShouXiangXiBiao
                            {
                                CaoZuoShiJian = DateTime.Now.ToString(),
                                XiaoShouDongZuo = Request.Form["zhuangtai"],
                                CaoZuoRen = Convert.ToInt32(HttpContext.Session.GetString("loginid")),
                                ZhuangTai = Request.Form["zhuangtai"],
                                XiaoShouBiaoId = _context.t_FangYuanKeHuGuanLianBiao.Where(x => x.Id == Convert.ToInt32(Request.Form["id"])).ToList()[0].XiaoShouBiaoId
                            };
                            if (it_XiaoShouXiangXiBiaoService.Add(t_XiaoShouXiangXiBiao).Id > 0)
                            {
                                panduan = 4;
                            }
                            else
                            {
                                panduan = -4;
                            };

                        }
                        break;
                    case 4://房屋信息修改
                        using (var _context = new MyContext(optionsBuilder2.Options))
                        {
                            int Id = _context.t_FangYuanKeHuGuanLianBiao.Where(x => x.Id == Convert.ToInt32(Request.Form["id"])).ToList()[0].FangYuanId;
                            var FangYuanXinXi = _context.t_FangYuanXinXiBiao.FirstOrDefault(x => x.Id == Id);
                            FangYuanXinXi.BeiZhu2 = DateTime.Now.ToString();
                            if (Request.Form["zhuangtai"] == "已签约")
                            {
                                FangYuanXinXi.ZhuangTai = "已出售";
                                if (_context.SaveChanges() > 0)
                                {
                                    panduan = 5;
                                }
                                else
                                {
                                    panduan = -5;
                                };
                            }
                            else if (Request.Form["zhuangtai"] == "已退房")
                            {
                                FangYuanXinXi.ZhuangTai = "未出售";
                                if (_context.SaveChanges() > 0)
                                {
                                    panduan = 5;
                                }
                                else
                                {
                                    panduan = -5;
                                };
                            }
                            else
                            {
                                if (FangYuanXinXi.ZhuangTai == "审核中")
                                {
                                    panduan = 5;
                                }
                                else
                                {
                                    FangYuanXinXi.ZhuangTai = "审核中";
                                    if (_context.SaveChanges() > 0)
                                    {
                                        panduan = 5;
                                    }
                                    else
                                    {
                                        panduan = -5;
                                    };
                                };
                            };
                        }
                        break;
                    case 5://房源客户修改
                        panduan = 6;
                        using (var _context = new MyContext(optionsBuilder3.Options))
                        {
                            var fangyuankehu = _context.t_FangYuanKeHuGuanLianBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["id"]));
                            //判断是否需要修改客户
                            if (fangyuankehu.BeiZhu1 == Request.Form["zhuangtai"])
                            {
                                panduan = 6;
                            }
                            else
                            {
                                fangyuankehu.BeiZhu1 = Request.Form["zhuangtai"];
                                if (_context.SaveChanges() > 0)
                                {
                                    panduan = 6;
                                }
                                else
                                {
                                    panduan = -6;
                                }
                            }
                        }
                        break;
                    default:
                        panduan = -1;//成功
                        break;
                }
            }
            return panduan;
        }
        #endregion
        #region 数据中心是否锁定小区
        [HttpPost]
        public int Suodingxiaoqu()
        {
            int panduan = 0;
            string fangyuan = it_KeHuBiaoService.GetList().Where(x => x.Id == Convert.ToInt32(Request.Form["kehuid"])).ToList()[0].YiXiangXinXi;
            if (fangyuan != null && fangyuan != "")
            {
                panduan = Convert.ToInt32(fangyuan);
            }
            if (panduan == 0)
            {
                return panduan;//等于零
            }
            else
            {
                return panduan;
            }
        }
        #endregion




        [HttpPost]
        public string UpdateKeHu()
        {
            bool b = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var KeHu = _context.t_KeHuBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["Id"]));
                KeHu.KeHuXingMing = Request.Form["XingMing"].ToString();
                KeHu.ShouJiHaoMa = Request.Form["ShouJiHaoMa"].ToString();
                KeHu.YiXiangDengJi = Request.Form["YiXiangDengJi"].ToString();
                KeHu.LianMingKeHu = Request.Form["LianMingKeHu"].ToString();
                KeHu.NianLing = Request.Form["NianLing"].ToString();
                KeHu.XingBie = Request.Form["XingBie"].ToString();
                KeHu.ZhengJianLeiXing = Request.Form["ZhengJianLeiXing"].ToString();
                KeHu.ZhengJianHaoMa = Request.Form["ZhengJianHaoMa"].ToString();
                KeHu.RenZhiMeiTi = Request.Form["RenZhiMeiTi"].ToString();
                KeHu.KeHuBeiZhu = Request.Form["KeHuBeiZhu"].ToString();
                KeHu.GeRenBiaoQian = Request.Form["GeRenBiaoQian"].ToString();
                KeHu.ZhiYeShenFen = Request.Form["ZhiYeShenFen"].ToString();
                KeHu.JiaTingJieGou = Request.Form["JiaTingJieGou"].ToString();
                KeHu.JiaoYuChengDu = Request.Form["JiaoYuChengDu"].ToString();
                KeHu.HunYinZhuangTai = Request.Form["HunYinZhuangTai"].ToString();
                KeHu.ShouRuQingKuang = Request.Form["ShouRuQingKuang"].ToString();
                KeHu.GongZuoQuYu = Request.Form["GongZuoQuYu"].ToString();
                KeHu.GOngZuoDiZhi = Request.Form["GOngZuoDiZhi"].ToString();
                KeHu.JuZhuQuYu = Request.Form["JuZhuQuYu"].ToString();
                KeHu.JuZhuDiZhi = Request.Form["JuZhuDiZhi"].ToString();
                KeHu.YiXiangYongTu = Request.Form["YiXiangYongTu"].ToString();
                KeHu.YiXiangYeTai = Request.Form["YiXiangYeTai"].ToString();
                KeHu.YiXiangHuXing = Request.Form["YiXiangHuXing"].ToString();
                KeHu.YiXiangLouCeng = Request.Form["YiXiangLouCeng"].ToString();
                KeHu.YiXiangMianJi = Request.Form["YiXiangMianJi"].ToString();
                KeHu.YiXiangTaoShu = Request.Form["YiXiangTaoShu"].ToString();
                KeHu.YiXiangDanJia = Request.Form["YiXiangDanJia"].ToString();
                KeHu.YiXiangZongJia = Request.Form["YiXiangZongJia"].ToString();
                KeHu.GuanZhuNeiRong = Request.Form["GuanZhuNeiRong"].ToString();
                KeHu.BeiZhu2 = Request.Form["slt_kehulaiyuan"].ToString();
                KeHu.RenYuanId = Convert.ToInt32(HttpContext.Session.GetString("loginid"));
                b = _context.SaveChanges() > 0;
            }
            if (b)
            {
                return "success";
            }
            else
            {
                return "error";
            }
        }

        [HttpPost]
        public int UpdateKeHu2()
        {
            int fangwuid = 0;
            int aid = 0;
            t_XiaoShouXiangXiBiao t_XiaoShouXiangXiBiao = new t_XiaoShouXiangXiBiao
            {
                CaoZuoShiJian = DateTime.Now.ToString(),
                XiaoShouDongZuo = Request.Form["zhuangtaiya"],
                CaoZuoRen = Convert.ToInt32(HttpContext.Session.GetString("loginid")),
                ZhuangTai = Request.Form["zhuangtaiya"]
            };
            var list = it_XiaoShouBiaoService.GetList().Where(x => x.KeHuId == Convert.ToInt32(Request.Form["Id"])).ToList();
            var fangwuxinxi = t_FangYuanXinXiBiaoService.GetFangWuBangDing(Convert.ToInt32(Request.Form["XiaoQuId"]), Request.Form["LouHao"], Convert.ToInt32(Request.Form["Ceng"])).Where(x => x.Men == Request.Form["Men"] && x.ZhuangTai == "未出售").ToList();
            if (fangwuxinxi.Count != 0)
            {
                fangwuid = fangwuxinxi[0].Id;
            }
            else
            {
                return -2;
            }
            if (list.Count != 0)
            {
                list[0].FangWuId = fangwuid;
                t_XiaoShouXiangXiBiao.XiaoShouBiaoId = list[0].Id;
                aid = list[0].Id;

                var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
                optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                using (var _context = new MyContext(optionsBuilder.Options))
                {
                    var XiaoShou = _context.t_XiaoShouBiao.FirstOrDefault(x => x.Id == list[0].Id);
                    var xiaoshouxiangxi = _context.t_XiaoShouXiangXiBiao.Where(x => x.XiaoShouBiaoId == XiaoShou.Id && x.XiaoShouDongZuo == "已认购" || x.XiaoShouDongZuo == "已认筹").ToList();
                    XiaoShou.BeiZhu1 = DateTime.Now.ToString();
                    XiaoShou.FangWuId = fangwuid;
                    if (xiaoshouxiangxi.Count > 0)
                    {
                        XiaoShou.ChengJiaoJinE = (Convert.ToInt32(Request.Form["ChengJiaoJinE"]) + Convert.ToInt32(XiaoShou.ChengJiaoJinE)).ToString();
                    }
                    else
                    {
                        XiaoShou.ChengJiaoJinE = Request.Form["ChengJiaoJinE"];
                    }
                    _context.SaveChanges();
                }
                using (var _context = new MyContext(optionsBuilder.Options))
                {
                    var FangYuanXinXi = _context.t_FangYuanXinXiBiao.FirstOrDefault(x => x.Id == fangwuid);
                    FangYuanXinXi.ZhuangTai = "审核中";
                    _context.SaveChanges();
                }
            }
            else
            {
                t_XiaoShouBiao t_XiaoShouBiao = new t_XiaoShouBiao();
                t_XiaoShouBiao.KeHuId = Convert.ToInt32(Request.Form["Id"]);
                t_XiaoShouBiao.YeWuYuanId = Convert.ToInt32(HttpContext.Session.GetString("loginid"));
                t_XiaoShouBiao.FangWuId = fangwuid;
                t_XiaoShouBiao.ChengJiaoJinE = Request.Form["ChengJiaoJinE"];
                t_XiaoShouBiao.BeiZhu1 = DateTime.Now.ToString();
                aid = t_XiaoShouXiangXiBiao.XiaoShouBiaoId = it_XiaoShouBiaoService.Add(t_XiaoShouBiao).Id;
            }

            t_FangYuanKeHuGuanLianBiao fykhglb = new t_FangYuanKeHuGuanLianBiao();
            fykhglb.KeHuId = Convert.ToInt32(Request.Form["Id"]);
            fykhglb.FangYuanId = fangwuid;
            fykhglb.ChengJiaoJinE = Request.Form["ChengJiaoJinE"];
            fykhglb.XiaoShouBiaoId = aid;
            t_FangYuanKeHuGuanLianBiao fykhglbs = new t_FangYuanKeHuGuanLianBiao();
            fykhglbs = it_XiaoShouBiaoService.fykhAdd(fykhglb);
            bool bo1 = false;
            var optionsBuilder0 = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder0.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder0.Options))
            {
                var KeHu = _context.t_KeHuBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["Id"]));
                KeHu.ZhuangTai = Request.Form["zhuangtaiya"];
                bo1 = _context.SaveChanges() > 0;
            }
            bool bo2 = it_XiaoShouXiangXiBiaoService.Add(t_XiaoShouXiangXiBiao).Id > 0;
            bool bo3 = false;
            using (var _context = new MyContext(optionsBuilder0.Options))
            {
                var XiaoShou = _context.t_XiaoShouBiao.FirstOrDefault(x => x.Id == aid);
                XiaoShou.FangWuId = fangwuid;
                bo3 = _context.SaveChanges() > 0;
            }
            if (true)
            {
                return Convert.ToInt32(Request.Form["Id"]);
            }
            else
            {
                return -1;
            }
        }

        public string ChuShiFangFaShu()
        {
            string id = Request.Form["shu"];
            List<t_FangYuanKeHuGuanLianBiao> shuju = it_KeHuBiaoService.GetListfykh(Convert.ToInt32(id));
            string fanhuizhi = "";
            for (int i = 0; i < shuju.Count; i++)
            {
                t_FangYuanXinXiBiao shuju1 = t_FangYuanXinXiBiaoService.GetId(shuju[i].FangYuanId);
                t_FangYuanBiao shuju2 = t_FangYuanBiaoService.GetIdAsync(shuju[i].FangYuanId);
                fanhuizhi += "<div class='col-md-6'><span>" + shuju2.XiaoQuMingCheng + "" + shuju1.LouHao + "号楼" + shuju1.Ceng + "层" + shuju1.Men + "门-成交金额:" + shuju[i].ChengJiaoJinE + "</span></div>";
            }
            return fanhuizhi;
        }

        public class biao
        {
            public int Id { get; set; }
            public string xingming { get; set; }
            public string yewuyuanxingming { get; set; }
            public string zhiyeguwenxingming { get; set; }
            public string haoma { get; set; }
            public string shijian { get; set; }
            public string shijian2 { get; set; }
        }
        public class zong
        {
            public int Id { get; set; }
        }

        [HttpPost]
        public string InitializationDataTable()
        {
            t_KeHuBiao t_KeHuBiao = new t_KeHuBiao();
            string wo = Request.Form["wo"];
            if (wo == null || wo == "" || wo == "undefined")
            {
                biao _biao = new biao();
                _biao.xingming = Request.Form["ipt_KeHuXingMing"];
                _biao.yewuyuanxingming = Request.Form["ipt_YeWuYuanMingCheng"];
                _biao.zhiyeguwenxingming = Request.Form["ipt_ZhYeGuWenMingCheng"];
                _biao.haoma = Request.Form["ipt_KeHuShouJiHao"];
                _biao.shijian = Request.Form["ipt_Kaishishijian"];
                _biao.shijian2 = Request.Form["ipt_JieShuShiJina"];
                //t_KeHuBiao.bei = TiaoJian;
                //t_KeHuBiao.ShouJiHaoMa = TiaoJian;
                //t_KeHuBiao.ZhengJianHaoMa = TiaoJian;
                //t_KeHuBiao.YiXiangDengJi = TiaoJian;
                //t_KeHuBiao.XingBie = TiaoJian;
                //t_KeHuBiao.LianMingKeHu = TiaoJian;
                return it_KeHuBiaoService.GetListWithFenYe(_biao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
            }
            else
            {
                t_KeHuBiao.Id = Convert.ToInt32(Request.Form["Id"]);
                t_KeHuBiao.KeHuXingMing = Request.Form["ipt_kehumingcheng"];
                t_KeHuBiao.ShouJiHaoMa = Request.Form["ipt_shoujihao"];
                return it_KeHuBiaoService.GetListWithFenYe2(t_KeHuBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
            }

            //t_KeHuBiao.t_RenYuanBiao.XingMing = Request.Form["ipt_renyuanmingcheng"];

        }

        [HttpPost]
        public string BaoBeiSubMit()
        {
            int KeHuId = Convert.ToInt32(Request.Form["KeHuId"]);
            int YeWuYuanId = Convert.ToInt32(HttpContext.Session.GetString("loginid"));
            int XiaoShouBiaoId = 0;
            string XiaoShouDongZuo = Request.Form["XiaoShouDongZuo"];
            string MiaoShu = Request.Form["MiaoShu"];


            List<t_XiaoShouBiao> listXiaoShouBiao = new List<t_XiaoShouBiao>();
            listXiaoShouBiao = it_XiaoShouBiaoService.GetList().Where(x => x.KeHuId == KeHuId).ToList();

            if (listXiaoShouBiao.Count == 0)
            {
                t_XiaoShouBiao t_XiaoShouBiao = new t_XiaoShouBiao();
                t_XiaoShouBiao.KeHuId = KeHuId;
                t_XiaoShouBiao.ShengChengShiJian = System.DateTime.Now.ToString();
                t_XiaoShouBiao.YeWuYuanId = YeWuYuanId;
                XiaoShouBiaoId = it_XiaoShouBiaoService.Add(t_XiaoShouBiao).Id;

            }
            else
            {
                XiaoShouBiaoId = listXiaoShouBiao[0].Id;
            }
            if (XiaoShouBiaoId == 0)
            {
                return "error";
            }
            t_XiaoShouXiangXiBiao t_XiaoShouXiangXiBiao = new t_XiaoShouXiangXiBiao();
            t_XiaoShouXiangXiBiao.XiaoShouDongZuo = XiaoShouDongZuo;
            t_XiaoShouXiangXiBiao.CaoZuoShiJian = System.DateTime.Now.ToString();
            t_XiaoShouXiangXiBiao.CaoZuoRen = YeWuYuanId;
            t_XiaoShouXiangXiBiao.XiaoShouBiaoId = XiaoShouBiaoId;
            t_XiaoShouXiangXiBiao.MiaoShu = MiaoShu;
            t_XiaoShouXiangXiBiao.ZhuangTai = "";
            int XiaoShouXiangXiBiaoId = it_XiaoShouXiangXiBiaoService.Add(t_XiaoShouXiangXiBiao).Id;

            if (XiaoShouXiangXiBiaoId > 0)
            {
                return "success";
            }
            else
            {
                return "error";
            }
        }
        [HttpPost]
        public string InitializationDataTableByKeHuId()
        {
            return Helper.ListToJsonAddFenYe(it_XiaoShouXiangXiBiaoService.GetByKeHuId(Convert.ToInt32(Request.Form["KeHuId"])), Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]));
        }
        [HttpPost]
        public string ReturnKeHuJson()
        {
            return JsonConvert.SerializeObject(new List<t_KeHuBiao> { it_KeHuBiaoService.GetById(Convert.ToInt32(Request.Form["Id"])) }, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        [HttpPost]
        public string BindSelXiaoQu()
        {
            return JsonConvert.SerializeObject(t_FangYuanBiaoService.GetList(), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        [HttpPost]
        public string BindSelLouHao()
        {
            return JsonConvert.SerializeObject(t_FangYuanXinXiBiaoService.GetLouHao(Convert.ToInt32(Request.Form["XiaoQuId"])), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        [HttpPost]
        public string BindSelCeng()
        {
            return JsonConvert.SerializeObject(t_FangYuanXinXiBiaoService.GetCeng(Convert.ToInt32(Request.Form["XiaoQuId"]), Request.Form["LouHao"]), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        [HttpPost]
        public string BindSelMen()
        {
            return JsonConvert.SerializeObject(t_FangYuanXinXiBiaoService.GetFangWuBangDing(Convert.ToInt32(Request.Form["XiaoQuId"]), Request.Form["LouHao"], Convert.ToInt32(Request.Form["Ceng"])), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }


        [HttpPost]
        public string BindFangYuanKeHuGuanLian()
        {
            //string wo = Request.Form["wo"];
            int start = Convert.ToInt32(Request.Form["start"]);
            int limit = Convert.ToInt32(Request.Form["limit"]);
            string KeHuXingMing = Request.Form["KeHuXingMing"];
            string DianHuaHaoMa = Request.Form["DianHuaHaoMa"];
            int YeWuYuanId = Convert.ToInt32(HttpContext.Session.GetString("loginid"));
            string strwhere = "where 1=1 AND t_FangYuanKeHuGuanLianBiao.BeiZhu1 != '已退房'";
            if (!string.IsNullOrEmpty(KeHuXingMing.Trim()))
            {
                strwhere += " and  KeHuXingMing ='" + KeHuXingMing + "'";
            }
            if (!string.IsNullOrEmpty(DianHuaHaoMa.Trim()))
            {
                strwhere += " and ShouJiHaoMa ='" + DianHuaHaoMa + "'";
            }
            //strwhere += " and t_KeHuBiao.RenYuanId=" + YeWuYuanId + "";
            string sql = "select t_FangYuanKeHuGuanLianBiao.Id as Id,t_FangYuanXinXiBiao.XiaoQuId as XiaoQuId,XiaoQuMingCheng,LouHao,Ceng,Men,KeHuXingMing,ChengJiaoJinE,ShouJiHaoMa,XiaoShouBiaoId,FangYuanId,t_FangYuanXinXiBiao.ZhuangTai as ZhuangTai,t_FangYuanKeHuGuanLianBiao.BeiZhu1 as BeiZhu1 from t_FangYuanKeHuGuanLianBiao inner join t_FangYuanXinXiBiao on t_FangYuanKeHuGuanLianBiao.FangYuanId=t_fangyuanxinxibiao.Id inner join t_FangYuanBiao on t_FangYuanXinXiBiao.XiaoQuId=t_FangYuanBiao.Id inner join t_KeHuBiao on t_fangyuankehuguanlianbiao.kehuid=t_kehubiao.id " + strwhere + "";
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var list1 = _context.Database.SqlQuery<KFFY>(sql).ToList();
                return Helper.ListToJsonAddFenYe(list1, start, limit);
            }




        }

        //删除房源客户关联数据，并修改房源信息状态
        [HttpPost]
        public string UpdateFangYuanXinXiAndShanChu()
        {
            bool bo1 = false;
            bool bo2 = false;
            bool bo3 = false;
            string a = Request.Form["GuanLianId"];
            int FangYuanKeHuGuanLianId = Convert.ToInt32(Request.Form["GuanLianId"]);

            string XiaoQuId = Request.Form["XiaoQuId"];
            string Dong = Request.Form["Dong"];
            string Ceng = Request.Form["Ceng"];
            string Men = Request.Form["Men"];
            string JinE = Request.Form["JinE"];

            var list1 = new List<t_FangYuanKeHuGuanLianBiao>();
            string sql = "select * from t_FangYuanKeHuGuanLianBiao where Id=" + FangYuanKeHuGuanLianId + "";
            var optionsBuilder1 = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder1.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder1.Options))
            {
                list1 = _context.Database.SqlQuery<t_FangYuanKeHuGuanLianBiao>(sql).ToList();
            }

            var fangwuxinxi = t_FangYuanXinXiBiaoService.GetFangWuBangDing(Convert.ToInt32(XiaoQuId), Dong, Convert.ToInt32(Ceng)).Where(x => x.Men == Men).ToList();
            string zt = "";
            if (fangwuxinxi[0].ZhuangTai == "未出售")
            {
                var optionsBuilder0 = new DbContextOptionsBuilder<MyContext>();
                optionsBuilder0.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                using (var _context = new MyContext(optionsBuilder0.Options))
                {
                    var fangyuanguanlian = _context.t_FangYuanKeHuGuanLianBiao.FirstOrDefault(x => x.Id == FangYuanKeHuGuanLianId);
                    var fangyuanxinxi = _context.t_FangYuanXinXiBiao.FirstOrDefault(x => x.Id == fangyuanguanlian.FangYuanId);
                    zt = fangyuanxinxi.ZhuangTai;
                    fangyuanxinxi.ZhuangTai = "未出售";

                    bo1 = _context.SaveChanges() > 0;
                }
                var optionsBuilder3 = new DbContextOptionsBuilder<MyContext>();
                optionsBuilder3.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                using (var _context = new MyContext(optionsBuilder3.Options))
                {
                    var fangyuanguanlian = _context.t_FangYuanKeHuGuanLianBiao.FirstOrDefault(x => x.Id == FangYuanKeHuGuanLianId);
                    fangyuanguanlian.FangYuanId = fangwuxinxi[0].Id;
                    fangyuanguanlian.ChengJiaoJinE = JinE;
                    bo2 = _context.SaveChanges() > 0;
                }
                var optionsBuilder4 = new DbContextOptionsBuilder<MyContext>();
                optionsBuilder4.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                using (var _context = new MyContext(optionsBuilder4.Options))
                {
                    var fangyuanxinxi = _context.t_FangYuanXinXiBiao.FirstOrDefault(x => x.Id == fangwuxinxi[0].Id);
                    fangyuanxinxi.ZhuangTai = zt;
                    bo3 = _context.SaveChanges() > 0;
                }

                if (bo1 && bo2 && bo3)
                {
                    return "success";
                }
                else
                {
                    return "error";
                }

            }
            else
            {
                if (list1[0].FangYuanId == fangwuxinxi[0].Id)
                {
                    //没有修改房屋信息
                    var optionsBuilder2 = new DbContextOptionsBuilder<MyContext>();
                    optionsBuilder2.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                    using (var _context = new MyContext(optionsBuilder2.Options))
                    {
                        var guanlian = _context.t_FangYuanKeHuGuanLianBiao.FirstOrDefault(x => x.Id == FangYuanKeHuGuanLianId);
                        guanlian.ChengJiaoJinE = JinE;
                        bool f = _context.SaveChanges() > 0;
                        if (f == true)
                        {
                            return "success";
                        }
                        else
                        {
                            return "Error";
                        }
                    }

                }
                else
                {
                    return "ZhuangTaiError";
                }
            }




        }


        //业务员
        public string Yewuyuan()
        {
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var list = new List<t_RenYuanBiao>();
                if (Request.Form["SelZhanDui"] == "undefined" || Request.Form["SelZhanDui"] == "0")
                {
                    list = _context.t_RenYuanBiao.Where(x => x.GangWeiId == 32).ToList();
                }
                else
                {
                    list = _context.t_RenYuanBiao.Where(x => x.GangWeiId == 32 && x.ZhanQuId == Convert.ToInt32(Request.Form["SelZhanQu"]) && x.ZhanDuiId == Convert.ToInt32(Request.Form["SelZhanDui"])).ToList();
                }
                if (Request.Form["Id"] != "undefined")
                {
                    int xiaoshouid = _context.t_FangYuanKeHuGuanLianBiao.Where(x => x.Id == Convert.ToInt32(Request.Form["Id"])).ToList()[0].XiaoShouBiaoId;
                    int yewuyuanid = _context.t_XiaoShouBiao.Where(x => x.Id == xiaoshouid).ToList()[0].YeWuYuanId;
                    list = _context.t_RenYuanBiao.Where(x => x.Id == yewuyuanid).ToList();
                }
                return JsonConvert.SerializeObject(list, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
            }

        }
        //置业顾问
        public string Zhhiyeguwen()
        {
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var list = new List<t_RenYuanBiao>();
                if (Request.Form["Id"] != "undefined")
                {
                    int xiaoshouid = _context.t_FangYuanKeHuGuanLianBiao.Where(x => x.Id == Convert.ToInt32(Request.Form["Id"])).ToList()[0].XiaoShouBiaoId;
                    int zhiyeguwenid = _context.t_XiaoShouBiao.Where(x => x.Id == xiaoshouid).ToList()[0].ZhiYeGuWenId;
                    list = _context.t_RenYuanBiao.Where(x => x.Id == zhiyeguwenid).ToList();
                }
                else
                {
                    list = _context.t_RenYuanBiao.Where(x => x.GangWeiId == 34).ToList();
                }

                return JsonConvert.SerializeObject(list, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
            }
        }


        [HttpPost]
        public string xiaoshouxiugai()
        {
            int panduan = 1;

            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));

            using (var _context = new MyContext(optionsBuilder.Options))
            {
                // 修改房源客户信息
                var kehufangyuanxinix = _context.t_FangYuanKeHuGuanLianBiao.Where(x => x.Id == Convert.ToInt32(Request.Form["GuanLianId"])).ToList()[0];
                while (panduan >= 1)
                {
                    switch (panduan)
                    {
                        case 1://客戶修改
                            //获取客户信息
                            var kehu = _context.t_KeHuBiao.FirstOrDefault(x => x.Id == kehufangyuanxinix.KeHuId);
                            //判断意向小区是否相同
                            if (kehu.YiXiangXinXi != Request.Form["XiaoQuId"])
                            {
                                kehu.YiXiangXinXi = Request.Form["XiaoQuId"];
                                //判断修改客户人员
                                if ("undefined" != Request.Form["yewuyuan"])
                                {
                                    kehu.RenYuanId = Convert.ToInt32(Request.Form["yewuyuan"]);
                                }
                                else
                                {
                                    kehu.RenYuanId = Convert.ToInt32(Request.Form["zhiyeguwen"]);
                                }
                                //客户是否修改成功
                                if (_context.SaveChanges() > 0)
                                {
                                    panduan = 2;
                                }
                                else
                                {
                                    panduan = -2;
                                }
                            }
                            else
                            {
                                //判断修改客户人员
                                if ("undefined" != Request.Form["yewuyuan"])
                                {
                                    if (kehu.RenYuanId != Convert.ToInt32(Request.Form["yewuyuan"]))
                                    {
                                        kehu.RenYuanId = Convert.ToInt32(Request.Form["yewuyuan"]);
                                        //客户是否修改成功
                                        if (_context.SaveChanges() > 0)
                                        {
                                            panduan = 2;
                                        }
                                        else
                                        {
                                            panduan = -2;
                                        }
                                    }
                                    else
                                    {
                                        panduan = 2;
                                    }
                                }
                                else
                                {
                                    if (kehu.RenYuanId != Convert.ToInt32(Request.Form["zhiyeguwen"]))
                                    {
                                        kehu.RenYuanId = Convert.ToInt32(Request.Form["zhiyeguwen"]);
                                        //客户是否修改成功
                                        if (_context.SaveChanges() > 0)
                                        {
                                            panduan = 2;
                                        }
                                        else
                                        {
                                            panduan = -2;
                                        }
                                    }
                                    else
                                    {
                                        panduan = 2;
                                    }
                                }
                            }
                            break;
                        case 2://销售修改
                            //房源客户信息
                            var fangyuankehuupdatebiao = _context.t_FangYuanKeHuGuanLianBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["GuanLianId"]));
                            //销售信息
                            var xiaoshou = _context.t_XiaoShouBiao.FirstOrDefault(x => x.Id == fangyuankehuupdatebiao.XiaoShouBiaoId);
                            //赋值 房源id
                            xiaoshou.FangWuId = kehufangyuanxinix.FangYuanId;


                            if ("undefined" != Request.Form["yewuyuan"])
                            {
                                xiaoshou.YeWuYuanId = Convert.ToInt32(Request.Form["yewuyuan"]);
                            }

                            xiaoshou.ZhiYeGuWenId = Convert.ToInt32(Request.Form["zhiyeguwen"]);

                            if (fangyuankehuupdatebiao.BeiZhu1 != "已退房")
                            {
                                xiaoshou.ChengJiaoJinE = (Convert.ToInt32(_context.t_FangYuanKeHuGuanLianBiao.Where(x => x.XiaoShouBiaoId == fangyuankehuupdatebiao.XiaoShouBiaoId && x.BeiZhu1 != "已退房" && x.Id != Convert.ToInt32(Request.Form["GuanLianId"])).ToList().Sum(x => Convert.ToInt32(x.ChengJiaoJinE))) + Convert.ToInt32(Request.Form["JinE"])).ToString();
                            }

                            xiaoshou.BeiZhu1 = DateTime.Now.ToString();
                            //判断销售是否修改成功
                            if (_context.SaveChanges() > 0)
                            {
                                panduan = 3;
                            }
                            else
                            {
                                panduan = -3;
                            }
                            break;
                        case 3://房源修改前数据
                               //用 房源id  查询 修改的房源客户信息
                            var fangyuanid = (t_FangYuanKeHuGuanLianBiao)_context.t_FangYuanKeHuGuanLianBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["GuanLianId"]));
                            //房源修改前信息
                            var fangyuanqian = _context.t_FangYuanXinXiBiao.FirstOrDefault(x => x.Id == fangyuanid.FangYuanId);
                            //房源修改后信息
                            var fangyuanhou = _context.t_FangYuanXinXiBiao.FirstOrDefault(x => x.XiaoQuId == Convert.ToInt32(Request.Form["XiaoQuId"]) && x.LouHao == Request.Form["Dong"].ToString() && x.Ceng == Convert.ToInt32(Request.Form["Ceng"]) && x.Men == Request.Form["Men"].ToString());
                            fangyuanhou.ZhuangTai = fangyuanqian.ZhuangTai;
                            //房源修改后是否成功
                            if (_context.SaveChanges() > 0)
                            {
                                panduan = 4;
                            }
                            else
                            {
                                panduan = -4;
                            }
                            if (fangyuanqian.ZhuangTai != "未出售")
                            {
                                //房源修改前是否成功
                                fangyuanqian.ZhuangTai = "未出售";
                                if (_context.SaveChanges() > 0)
                                {
                                    panduan = 4;
                                }
                                else
                                {
                                    panduan = -7;
                                }
                            }
                            else
                            {
                                panduan = 4;
                            }

                            break;
                        case 4://房源客户修改

                            //用 房源id  查询 修改的房源客户信息
                            var fangyuankehuupdate = _context.t_FangYuanKeHuGuanLianBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["GuanLianId"]));

                            //查询 出要修改的房子
                            var fangyuanxinxibiao = _context.t_FangYuanXinXiBiao.Where(x => x.XiaoQuId == Convert.ToInt32(Request.Form["XiaoQuId"]) && x.LouHao == Request.Form["Dong"].ToString() && x.Ceng == Convert.ToInt32(Request.Form["Ceng"]) && x.Men == Request.Form["Men"].ToString()).ToList()[0];
                            fangyuankehuupdate.FangYuanId = fangyuanxinxibiao.Id;
                            fangyuankehuupdate.ChengJiaoJinE = Request.Form["JinE"];
                            fangyuankehuupdate.BeiZhu2 = DateTime.Now.ToString();
                            if (_context.SaveChanges() > 0)
                            {
                                panduan = 5;
                            }
                            else
                            {
                                panduan = -5;
                            }
                            break;
                        case 5://销售详细修改
                            //用 房源id  查询 修改的房源客户信息
                            var fangyuankehuupdatebiao2 = _context.t_FangYuanKeHuGuanLianBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["GuanLianId"]));

                            //查询 出要修改的房子
                            t_XiaoShouXiangXiBiao t_XiaoShouXiangXiBiao2 = new t_XiaoShouXiangXiBiao
                            {
                                XiaoShouDongZuo = fangyuankehuupdatebiao2.BeiZhu1,
                                CaoZuoShiJian = DateTime.Now.ToString(),
                                CaoZuoRen = Convert.ToInt32(HttpContext.Session.GetString("loginid")),
                                XiaoShouBiaoId = fangyuankehuupdatebiao2.XiaoShouBiaoId,
                                ZhuangTai = fangyuankehuupdatebiao2.BeiZhu1,
                                MiaoShu = ""
                            };
                            //销售详细添加签约判断
                            _context.t_XiaoShouXiangXiBiao.Add(t_XiaoShouXiangXiBiao2);
                            if (_context.SaveChanges() > 0)
                            {
                                panduan = 6;
                            }
                            else
                            {
                                panduan = -6;
                            }
                            break;
                        default:
                            panduan = -1;
                            break;
                    }
                }
            }
            return panduan.ToString();
        }

    }
    public class KFFY
    {
        public int Id { get; set; }
        public int XiaoQuId { get; set; }
        public string XiaoQuMingCheng { get; set; }
        public string LouHao { get; set; }
        public int Ceng { get; set; }
        public string Men { get; set; }
        public string KeHuXingMing { get; set; }
        public string ChengJiaoJinE { get; set; }
        public string ShouJiHaoMa { get; set; }
        public int XiaoShouBiaoId { get; set; }
        public int FangYuanId { get; set; }
        public string ZhuangTai { get; set; }
        public string BeiZhu1 { get; set; }
    }
}
